package com.uxwine.view.t;

import com.uxwine.alarm.R;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;

public class TimeView extends FrameLayout {
	
	private Bitmap[] mSelectBitmaps;
	private Bitmap[] mNormalBitmaps;	
	private DigitalView[] mDViews;
	private TimerViewAdpater mAdapter;
	private OnTimeViewClickedListener mOTVCListener;
	
	private int mnMarkerWidth;
	private int mnMarkerHeight;
	
	private boolean mbShowMarker = false;
	
	private float mnTSMarkerRate = 20.0f / 128.0f;

	public TimeView(Context context, AttributeSet attrs) {
		super(context, attrs);
		initView(context);
	}
	
	public void setAdapter(TimerViewAdpater adapter)
	{
		mAdapter = adapter;
		mAdapter.setAdapterView(this);
	}
	
	public void setShowMarker(boolean isShowMarker)
	{
		mbShowMarker = isShowMarker;
	}
	
	public void updateView()
	{
		int count = mDViews.length;
		for(int i = 0; i < count; i++)
		{
			updateChild(i);
		}
	}
	
	public void setOnTimeViewClikedListener(OnTimeViewClickedListener l)
	{
		mOTVCListener = l;
	}
	
	private void updateChild(int idx)
	{
		DigitalView dv = mDViews[idx];
		if(isEnable(idx))
		{
			dv.setEnabled(true);
			dv.setSelected(isSelected(idx));			
		}
		else
		{
			dv.setEnabled(false);
		}
		dv.setShowMarker(mbShowMarker && isCurrent(idx));
		dv.setValue(getValue(idx));
		dv.updateView();
	}
	
	private boolean isEnable(int index)
	{
		if(null != mAdapter)
			return mAdapter.isEnable(index);
		return true;
	}
	
	private boolean isSelected(int index)
	{
		if(null != mAdapter)
			return mAdapter.isSelected(index);
		return false;
	}
	
	private boolean isCurrent(int index)
	{
		if(null != mAdapter)
			return mAdapter.isCurrent(index);
		return false;
	}
	
	private int getValue(int index)
	{
		if(null != mAdapter)
			return mAdapter.getValue(index);
		return 0;
	}

		
	private void initView(Context context)
	{
		final LayoutInflater l = LayoutInflater.from(context);
		l.inflate(R.layout.view_time_span, this);
		
		mSelectBitmaps = loadBitmaps(10, "time_digital_62_");		
		mNormalBitmaps = loadBitmaps(10, "time_digital_88_");
		
		mDViews = new DigitalView[5];
		mDViews[0] = (DigitalView)findViewById(R.id.time_span_year);
		mDViews[1] = (DigitalView)findViewById(R.id.time_span_month);
		mDViews[2] = (DigitalView)findViewById(R.id.time_span_date);
		mDViews[3] = (DigitalView)findViewById(R.id.time_span_hour);
		mDViews[4] = (DigitalView)findViewById(R.id.time_span_minute);
		
//		mnMarkerWidth = mivMarker.getWidth();
//		mnMarkerHeight = mivMarker.getHeight();
		
		int count = mDViews.length;
		for(int i = 0; i < count; i++)
		{
			DigitalView v = mDViews[i];
			v.setOnClickListener(new MListener(i));
			v.setBitmaps(mNormalBitmaps, mSelectBitmaps, null);
		}
	}

	private Bitmap[] loadBitmaps(int count, String prefix)
	{
		final Context context = getContext();
		final Resources r = context.getResources();
		final String pkgName = context.getPackageName();
		Bitmap[] bms = new Bitmap[count];
		for(int i = 0; i < count; i++)
		{
			int id = r.getIdentifier(prefix + i, "drawable", pkgName);
			bms[i] = BitmapFactory.decodeResource(r, id);
		}
		return bms;
	}
	
	public static interface OnTimeViewClickedListener
	{
		public void onSpanClick(TimeView v, int index);
	}
	
	private class MListener implements View.OnClickListener
	{
		private int mIndex = 0;
		public MListener(int idx)
		{
			mIndex = idx;
		}
		@Override
		public void onClick(View v) {
			if(null != mOTVCListener)
				mOTVCListener.onSpanClick(TimeView.this, mIndex);
		}		
	}	
	
	public static interface TimerViewAdpater
	{
		public void setAdapterView(TimeView tv);
		public void notifyDataChanged();
		public boolean isEnable(int index);		
		public boolean isSelected(int index);		
		public boolean isCurrent(int index);		
		public int getValue(int index);
	}
}
